package com.hdaccp.lettingagency.controller;

import com.hdaccp.lettingagency.dao.RoleDao;
import com.hdaccp.lettingagency.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@Scope("prototype")
public class RoleController {
    @Autowired
    private RoleDao roleDao  ;
    @RequestMapping("/queryRolePage")
    @ResponseBody
    public Map<String,Object> queryRolePage(@RequestParam(value = "name",required = false,defaultValue = "") String name,int limit,int offset){
        Map<String,Object> map = new HashMap<>() ;
        map.put("total",roleDao.getTotalCount(name)) ;
        map.put("rows",roleDao.queryPage(name,limit,offset)) ;
        return map ;
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<Role> query(String name){
        Role r = roleDao.getRoleByUserName(name) ;
        List<Role> list = roleDao.query() ;
        for(Role role :list){
            if(role.getId()==r.getId()){
                role.setIschekced(true);
            }

        }
       return list ;
    }
}
